package com.lee.dhsg.dao.provider;

import com.lee.dhsg.domain.Course;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author Jun
 */
public class CourseSqlProvider {
    /**
     * 动态查询课题数量
     * @param params
     * @return
     */
    public String count(Map<String, Object> params) {
        return new SQL() {
            {
                SELECT("count(*)");
                FROM("course");
                if (params.get("course") != null) {
                    Course course = (Course) params.get("course");
                    if (course.getCategory() != null && course.getCategory().getCategoryid() != null && course.getCategory().getCategoryid() != 0) {
                        WHERE("categoryid=#{course.category.categoryid}");
                    }
                }
            }
        }.toString();
    }

    /**
     * 动态查询课题
     * @param params
     * @return
     */
    public String selectWhitParam(Map<String, Object> params) {
        return new SQL() {
            {
                SELECT("*");
                FROM("course");
                if (params.get("course") != null) {
                    Course course = (Course) params.get("course");
                    if(course.getCategory() != null && course.getCategory().getCategoryid() != null && course.getCategory().getCategoryid() != 0){
                        WHERE("categoryid=#{course.category.categoryid}");
                    }
                    if (course.getTeacher() != null && course.getTeacher().getTid() != null && course.getTeacher().getTid() != 0) {
                        WHERE("tid=#{course.teacher.tid}");
                    }
                    if(course.getCname()!=null&&!course.getCname().equals("")){
                        WHERE("cname LIKE CONCAT ('%',#{course.cname},'%')");
                    }
                    if(course.getTuijian()!=null&&!course.getTuijian().equals("")){
                        WHERE("tuijian = #{course.tuijian} ");
                    }
                }
            }
        }.toString();
    }

    /**
     * 动态插入课题
     * @param course
     * @return
     */
    public String insertCourse(Course course){
        return new SQL(){
            {
                INSERT_INTO("course");
                if(course.getTeacher()!=null){
                    VALUES("tid","#{teacher.tid}");
                }
                if(course.getCategory()!=null){
                    VALUES("categoryid","#{category.categoryid}");
                }
                if(course.getCname()!=null){
                    VALUES("cname","#{cname}");
                }
                if(course.getImg()!=null){
                    VALUES("img","#{img}");
                }
                if(course.getOriginaPrice()!=null){
                    VALUES("originaPrice","#{originaPrice}");
                }
                if(course.getDiscountPrice()!=null){
                    VALUES("discountPrice","#{discountPrice}");
                }
                if(course.getDiscount()!=null){
                    VALUES("discount","#{discount}");
                }
                if (course.getReducePrice()!=null){
                    VALUES("reducePrice","#{reducePrice}");
                }
                if (course.getDuration()!=null){
                    VALUES("duration","#{duration}");
                }
                if (course.getOnlineTime()!=null){
                    VALUES("onlineTime","#{onlineTime}");
                }
                if (course.getCourseDescription()!=null){
                    VALUES("courseDescription","#{courseDescription}");
                }
                if (course.getResource()!=null){
                    VALUES("resource","#{resource}");
                }
                if (course.getDiscountDescription()!=null){
                    VALUES("discountDescription","#{discountDescription}");
                }
                if (course.getDiscountBeginTime()!=null){
                    VALUES("discountBeginTime","#{discountBeginTime}");
                }
                if (course.getDiscountStopTime()!=null){
                    VALUES("discountStopTime","#{discountStopTime}");
                }
                if (course.getDiscountStatus()!=null){
                    VALUES("discountStatus","#{discountStatus}");
                }

            }
        }.toString();
    }

    /**
     * 动态更新课题
     * @param course
     * @return
     */
    public String updateCourse(Course course){
        return new SQL(){
            {
                UPDATE("course");
                if(course.getCname()!=null){
                    SET("cname=#{cname}");
                }
                if(course.getImg()!=null){
                    SET("img=#{img}");
                }
                if(course.getOriginaPrice()!=null){
                    SET("originaPrice=#{originaPrice}");
                }
                if(course.getDiscountPrice()!=null){
                    SET("discountPrice=#{discountPrice}");
                }
                if(course.getDiscount()!=null){
                    SET("discount=#{discount}");
                }
                if (course.getReducePrice()!=null){
                    SET("reducePrice=#{reducePrice}");
                }
                if (course.getDuration()!=null){
                    SET("duration=#{duration}");
                }
                if (course.getOnlineTime()!=null){
                    SET("onlineTime=#{onlineTime}");
                }
                if (course.getCourseDescription()!=null){
                    SET("courseDescription=#{courseDescription}");
                }
                if (course.getResource()!=null){
                    SET("resource=#{resource}");
                }
                if (course.getDiscountDescription()!=null){
                    SET("discountDescription=#{discountDescription}");
                }
                if (course.getDiscountBeginTime()!=null){
                    SET("discountBeginTime=#{discountBeginTime}");
                }
                if (course.getDiscountStopTime()!=null){
                    SET("discountStopTime=#{discountStopTime}");
                }
                if (course.getDiscountStatus()!=null){
                    SET("discountStatus=#{discountStatus}");
                }
                WHERE("courseid=#{courseid}");
            }
        }.toString();
    }
}
